package com.hcy.controller;

import com.hcy.common.dto.request.PushMsgRequestDTO;
import com.hcy.commonn.CommonInfoClient;
import com.hcy.ordermodel.dto.request.CustomerOrderListRequestDTO;
import com.hcy.orderclient.OrderBaseClient;
import com.hcy.ordermodel.dto.request.PayOrderRequestDTO;
import com.hcy.ordermodel.dto.response.PayOrderResponseDTO;
import com.hcy.ordermodel.dto.response.SimpleOrderResponseDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.enums.Master;
import com.hcy.rabbitbasicmodel.enums.OrderStatus;
import com.hcy.rabbitbasicmodel.enums.PushMsg;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicutils.utils.MyDateUtils;
import com.hcy.userclient.client.UserInfoClient;
import com.hcy.usermodel.dto.request.EvaluationRequestDTO;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/7 15:35
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    OrderBaseClient orderBaseClient;
    @Autowired
    UserInfoClient userInfoClient;
    @Autowired
    CommonInfoClient commonInfoClient;
    /**
     * @Author huchenying
     * @Description 支付 payNumber
     * @Date 2022/4/7 15:36
     **/
    @PostMapping("/payOrderByCoin")
    public ReturnResult payOrderByCoin(@RequestBody PayOrderRequestDTO requestDTO) {
        logger.info("payOrderByCoin start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        if (StringUtils.isNullOrEmpty(requestDTO.getPayNumber())) {
            return ReturnResult.fail("payNumber 不能为空！");
        }
        try {
            EvaluationRequestDTO evaluationRequestDTO = new EvaluationRequestDTO();
            BeanUtils.copyProperties(requestDTO,evaluationRequestDTO);
            evaluationRequestDTO.setOrderId(requestDTO.getOrderNumber());
            ResponseData<Object> evaluation = userInfoClient.evaluation(evaluationRequestDTO);
            if(evaluation.getCode().noSuccess()){
                logger.error("评价信息添加失败");
            }

            ResponseData<SimpleOrderResponseDTO> responseData = orderBaseClient.payOrderByCoin(requestDTO);
            if (responseData.getCode() != Code.Success) {
                return ReturnResult.fail("支付失败！");
            }
            SimpleOrderResponseDTO serviceOne = responseData.getData();
            //请求pushMessage
            PushMsgRequestDTO pushMsgRequestDTO = new PushMsgRequestDTO();
            //可抽象成工具类
            pushMsgRequestDTO.setContent("订单" + serviceOne.getOrderNumber() + " 已完成支付");
            pushMsgRequestDTO.setMaster(Master.Driver.getCode());
            pushMsgRequestDTO.setTime(new Date());
            pushMsgRequestDTO.setTypeId(PushMsg.Pay.getCode());
            pushMsgRequestDTO.setType(PushMsg.Pay.getName());
            pushMsgRequestDTO.setDriverName(serviceOne.getDriverPhone());
            ResponseData<Object> responseData1 = commonInfoClient.pushMessage(pushMsgRequestDTO);
            if(responseData1.getCode() != Code.Success){
                logger.warn("消息推送失败！");
            }
            return ReturnResult.success().setData(responseData);
        } catch (Exception e) {
            logger.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("支付失败！");
        }
    }

    /**
     * @Author huchenying
     * @Description 查未支付订单
     * @Date 2022/4/7 15:36
     **/
    @PostMapping("/getNoPayOrder")
    public ReturnResult getNoPayOrder(@RequestBody PayOrderRequestDTO requestDTO){
        logger.info("getNoPayOrder start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        if(requestDTO.getCustomerId() == null){
            return ReturnResult.fail("参数校验失败！");
        }
        try {
            requestDTO.setStatus(0);
            ResponseData<PayOrderResponseDTO> payOrder = orderBaseClient.getNoPayOrder(requestDTO);
            if (payOrder.getCode() != Code.Success) {
                return ReturnResult.fail("获取未支付订单失败！");
            }
            return ReturnResult.success().setData(payOrder.getData());
        } catch (Exception e) {
            logger.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("获取未支付订单失败！");
        }
    }


    /**
     * @Author huchenying
     * @Description 历史订单列表
     * @Date 2022/4/7 15:36
     **/
    @PostMapping("/getOrderList")
    public ReturnResult getOrderList(@Validated @RequestBody CustomerOrderListRequestDTO requestDTO){
        logger.info("getOrderList start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        try {
            List<Integer> status = new ArrayList<>();
            status.add(OrderStatus.CANCEL.getCode());
            status.add(OrderStatus.PAYMENT.getCode());
            status.add(OrderStatus.INIT_PAYMENT.getCode());
            status.add(OrderStatus.ARRIVE.getCode());
            requestDTO.setStatus(status);
            ResponseData<List<SimpleOrderResponseDTO>> orderList = orderBaseClient.getOrderList(requestDTO);
            if (orderList.getCode() != Code.Success) {
                return ReturnResult.fail("获取未支付订单失败！");
            }
            return ReturnResult.success().setData(orderList.getData());
        } catch (Exception e) {
            logger.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("获取订单列表失败！");
        }
    }

    /**
     * @Author huchenying
     * @Description 评价
     * @Date 2022/4/7 15:36
     **/
    @PostMapping("/evaluation")
    public ReturnResult evaluation(@Validated @RequestBody EvaluationRequestDTO requestDTO){
        logger.info("evaluation start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> evaluation = userInfoClient.evaluation(requestDTO);
            if (evaluation.getCode() != Code.Success) {
                return ReturnResult.fail("评价失败！");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            logger.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("评价失败！");
        }
    }
}
